package com.bluetrum.abmate.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetrum.abmate.R;
import com.bluetrum.abmate.databinding.CardDeviceFunctionBinding;
import com.bluetrum.abmate.musicplayer.MusicPlayerActivity;
import com.bluetrum.devicemanager.models.ABDevice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BtDeviceFunctionsAdapter extends RecyclerView.Adapter<BtDeviceFunctionsAdapter.ViewHolder>
        implements View.OnClickListener {

    private static final List<DeviceFunctionUnit> allFunctionUnits = Collections.unmodifiableList(Arrays.asList(
            new DeviceFunctionUnit(R.string.home_function_local_music, R.mipmap.ic_function_local_music, MusicPlayerActivity.class)
    ));

    private int mSpanCount;
    private int mParentWidth;

    private ABDevice mDevice;

    private List<DeviceFunctionUnit> functionUnits;

    private OnItemClickListener mOnItemClickListener;

    public BtDeviceFunctionsAdapter(ABDevice device, int spanCount) {
        this.mSpanCount = spanCount;
        this.mDevice = device;
        functionUnits = parseFunctionUnits(mDevice);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mParentWidth = parent.getWidth();

        CardDeviceFunctionBinding binding = CardDeviceFunctionBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 正方形
        int edge = mParentWidth / mSpanCount;
        holder.containerView.getLayoutParams().width = edge;
        holder.containerView.getLayoutParams().height = edge;

        // 填充内容，设置功能文字和图像等
        DeviceFunctionUnit functionUnit = allFunctionUnits.get(position);

        holder.functionImageView.setImageResource(functionUnit.getImageResId());
        holder.functionTitleTextView.setText(functionUnit.getTextResId());

        holder.functionImageView.setTag(functionUnit);

        holder.functionImageView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return functionUnits.size();
    }

    public void setSpanCount(int spanCount) {
        this.mSpanCount = spanCount;
        notifyDataSetChanged();
    }

    public void setBtDevice(ABDevice device) {
        // 只有设备发生变化才会更新
        if ((mDevice != null && device == null)
                || (mDevice == null && device != null)) {
            this.mDevice = device;
            functionUnits = parseFunctionUnits(mDevice);
            notifyDataSetChanged();
        }
    }

    private List<DeviceFunctionUnit> parseFunctionUnits(ABDevice device) {
        ArrayList<DeviceFunctionUnit> units = new ArrayList<>();

        if (device != null) {
            // todo: 解析设备功能
            units.add(allFunctionUnits.get(0));
        } else {
            units.addAll(allFunctionUnits);
        }

        return units;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            DeviceFunctionUnit functionUnit = (DeviceFunctionUnit) v.getTag();
            mOnItemClickListener.OnItemClick(functionUnit);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View containerView;
        View contentView;
        ImageView functionImageView;
        TextView functionTitleTextView;

        public ViewHolder(final CardDeviceFunctionBinding binding) {
            super(binding.getRoot());
            containerView = binding.getRoot();
            contentView = binding.contentView;
            functionImageView = binding.functionImageView;
            functionTitleTextView = binding.functionTitle;
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(DeviceFunctionUnit functionUnit);
    }

}
